package com.nt.train.controller;

import com.nt.train.dto.OrderInfo;
import com.nt.train.dto.PayResult;
import com.nt.train.service.IPaymentService;
import com.nt.train.service.context.PayStrategyContext;
import com.nt.train.service.context.SimpleContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 外部调用接口
 * @author hlf
 * @version 1.0
 * @date 2022/7/28 8:51
 */
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private SimpleContext simpleContext;

    @Resource
    PayStrategyContext payContext;

    /**
     * 支付接口 下单  中级水平处理
     * @param amount
     * @param paymentType
     * @return
     */
    @RequestMapping("/toPay")
    public PayResult pay(@RequestParam("amount") BigDecimal amount,
                         @RequestParam("paymentType") String paymentType) {
        OrderInfo order = new OrderInfo();
        order.setAmount(amount);
        order.setPaymentType(paymentType);
        //根据支付类型获取对应的策略 bean
        IPaymentService payment = applicationContext.getBean(order.getPaymentType(), IPaymentService.class);
        // 开始支付
        PayResult payResult = payment.pay(order);
        return payResult;
    }

    @GetMapping("/choose")
    public String choose(@RequestParam String poolId){
        return simpleContext.getResource(poolId);
    }

//    @RequestMapping("/ithdraw")
//    public PayResult pay(@RequestParam("amount") BigDecimal amount,
//                         @RequestParam("paymentType") String paymentType) {
//        OrderInfo order = new OrderInfo();
//        order.setAmount(amount);
//        order.setPaymentType(paymentType);
//        //根据支付类型获取对应的策略 bean
//        IPaymentService payment = applicationContext.getBean(order.getPaymentType(), IPaymentService.class);
//        // 开始支付
//        PayResult payResult = payment.pay(order);
//        return payResult;
//    }

    @GetMapping("/useStrategy")
    public void useStrategy(@RequestParam(value = "mode") String mode) {
        payContext.useStrategy(mode, new BigDecimal(10));
    }

}
